<template>
  <view class="container">
    <!--背景-->
    <view class="bg-content">
      <image class="bg" src="/static/image/v2/bg.png"></image>
      <view class="my-word" v-if="userInfo.personal_signature">
        <view class="my-word-content">{{ userInfo.personal_signature }}</view>
      </view>
      <view class="bg-null"></view>
      <view class="avatar-container" v-if="userInfo.nickname">
        <image class="avatar" src='https://image.qiuhuiyi.cn/tmp/lDNJq4jcXlNM5f7a75fa69e3b8ee44a4222fb46cac98.jpeg'>
        </image>
        <image class="header-avatar-border" src="/static/image/v2/border.png"></image>
        <image class="boy" wx:if="{{true}}" src="/static/image/v2/boy.png"></image>
        <image class="girl" wx:if="{{false}}" src="/static/image/v2/girl.png"></image>
      </view>
      <view class="avatar-container" v-if="!userInfo.nickname">
        <view class="login-button" v-if="!userInfo.nickname">
          <button @click="getAuthUserInfo">登录</button>
        </view>
      </view>

    </view>
    <!--个人信息-->
    <view class="user-info">
      <view class="fans" bindtap="openFollowList" data-t="2">
        <view>{{ userInfo.fans_num }}</view>
        <view>粉丝</view>
      </view>
      <view class="nickname">
        <view v-if="userInfo.nickname" class="nickname-data">{{ userInfo.nickname }}</view>
      </view>
      <view class="follow" bindtap="openFollowList" data-t="1">
        <view>{{ userInfo.follow_num }}</view>
        <view>关注</view>
      </view>
    </view>
    <!-- 步数信息 -->
    <view class="user-info-content">
      <view class="info-top">
        <view class="today-step user-info-item">
          <view class="user-info-image">
            <view class="image-content p1">
              <image src="/static/image/v2/p1.png"></image>
            </view>
          </view>
          <view class="user-info-data">
            <view class="user-info-flag">今日步数</view>
            <view class="user-info-word">{{ userInfo.today_step }}</view>
          </view>
        </view>
        <view class="today-rand user-info-item">
          <view class="user-info-image">
            <view class="image-content p2">
              <image src="/static//image/v2/p2.png"></image>
            </view>
          </view>
          <view class="user-info-data">
            <view class="user-info-flag">今日排名</view>
            <view class="user-info-word">{{ 3 }}</view>
          </view>
        </view>
      </view>
      <view class="info-top">
        <view class="today-step user-info-item">
          <view class="user-info-image">
            <view class="image-content p3">
              <image src="/static//image/v2/p3.png"></image>
            </view>
          </view>
          <view class="user-info-data">
            <view class="user-info-flag">帖子动态</view>
            <view class="user-info-word">{{ userInfo.post_num }}</view>
          </view>
        </view>
        <view class="today-rand user-info-item">
          <view class="user-info-image">
            <view class="image-content p4">
              <image src="/static//image/v2/p4.png"></image>
            </view>
          </view>
          <view class="user-info-data">
            <view class="user-info-flag">打卡天数</view>
            <view class="user-info-word">{{ userInfo.clock_num }}</view>
          </view>
        </view>
      </view>
    </view>

  </view>
</template>

<script setup >
import Request from '../../utils/request'
import config from '../../config'
import { ref } from 'vue';
import { onShow } from '@dcloudio/uni-app'
const userInfo = ref({
  nickname: '',
  avatar: '',
  personal_signature: '',
  nickname: '',
  fans_num: 0,
  follow_num: 0,
  post_num: 0,
  clock_num: 0,
  today_step: 0,
  total_step: 0
})
const requestService = new Request()

// 点击登录按钮
const getAuthUserInfo = () => {
  uni.getUserProfile({
    desc: '登录后可修改信息',
    success(res) {
      uni.login({
        success(e) {
          requestService.post({
            url: '/auth/login_v2?type=weChat',
            data: {
              encrypted_data: res.encryptedData,
              code: e.code,
              iv: res.iv,
              app_id: config.alianceKey
            }
          }).then((res) => {
            uni.setStorageSync('token', res.data)
          }).then(() => {
            requestService.get({
              url: '/personal_info',
              data: {}
            }).then((res) => {
              console.log(res);
              userInfo.value.avatar = res.data.avatar
              userInfo.value.nickname = res.data.nickname
              userInfo.value.personal_signature = res.data.personal_signature
              userInfo.value.fans_num = res.data.fans_num
              userInfo.value.follow_num = res.data.follow_num
              userInfo.value.post_num = res.data.post_num
              userInfo.value.clock_num = res.data.clock_num
            })
          })
        }
      })
      setTimeout(() => {
        uni.navigateTo({
          url: '/pages/personal/message/index'
        })
      }, 1500)
    },
    fail(e) {
      console.log(12, e);
    }
  })
}

// 获取个人信息
const getUserInfoShow = () => {
  requestService.get({
    url: '/personal_info',
    data: {}
  }).then((res) => {
    console.log(res);
    userInfo.value.avatar = res.data.avatar
    userInfo.value.nickname = res.data.nickname
    userInfo.value.personal_signature = res.data.personal_signature
    userInfo.value.fans_num = res.data.fans_num
    userInfo.value.follow_num = res.data.follow_num
    userInfo.value.post_num = res.data.post_num
    userInfo.value.clock_num = res.data.clock_num
  })
}

// 获取步数
const getStep = async () => {
  let res = await requestService.get({
    url: '/run_statistic',
    data: {}
  })
  console.log(res.data);
  userInfo.value.today_step = res.data.today_step
  userInfo.value.total_step = res.data.total_step

}

// 页面显示时
onShow(() => {
  getUserInfoShow()
  getStep()
})
</script>

<style scoped lang="less">
.container {
  width: 100%;
  display: flex;
  flex-direction: column;

  .bg-content {
    width: 100%;
    position: relative;

    .bg {
      width: 100%;
      height: 600rpx;
      display: flex;
      flex-direction: column;
      align-items: center;
    }

    .my-word {
      position: absolute;
      z-index: 300;
      height: 130rpx;
      width: 100%;
      display: flex;
      flex-direction: column;
      align-items: center;
      top: 250rpx;

      .my-word-content {
        width: 50%;
        padding: 20rpx 50rpx;
        font-size: 25rpx;
        font-family: OPPOSans, OPPOSans-Regular;
        font-weight: 400;
        text-align: CENTER;
        color: #ffffff;
        line-height: 28rpx;
        background: rgba(0, 0, 0, 0.10);
        border-radius: 20rpx;
        backdrop-filter: blur(3rpx);
      }
    }

    .bg-null {
      width: 100%;
      height: 100rpx;
      background: #ffffff;
      position: absolute;
      z-index: 100;
      bottom: -5rpx;
      border-radius: 50% 50% 0 0 / 90% 90% 0 0;

    }

    .avatar-container {
      width: 100%;
      height: 150rpx;
      position: absolute;
      z-index: 500;
      display: flex;
      flex-direction: column;
      align-items: center;
      justify-content: center;
      bottom: 20rpx;

      .avatar {
        width: 150rpx;
        height: 150rpx;
        border-radius: 35rpx;
        box-shadow: 0px 0px 50rpx -10rpx rgba(193, 193, 193, 0.71);
        border: 5rpx solid #303030;

      }

      .header-avatar-border {
        width: 100rpx;
        height: 100rpx;
        position: absolute;
        z-index: 600;
        bottom: 75rpx;
        left: 275rpx;
      }

      .girl {
        width: 55rpx;
        height: 55rpx;
        position: absolute;
        z-index: 600;
        bottom: 75rpx;
        left: 275rpx;
      }

      .boy {
        width: 55rpx;
        height: 55rpx;
        position: absolute;
        z-index: 600;
        bottom: -5rpx;
        left: 290rpx;
      }

      .login-button button {
        background: #fbc346;
        font-size: 30rpx;
        font-family: SF Pro Text, SF Pro Text-Bold;
        font-weight: 700;
        text-align: CENTER;
        color: #303030;
      }
    }
  }

  .user-info {
    width: 100%;
    display: flex;
    flex-direction: row;
    justify-content: space-around;

    .fans {
      font-size: 30rpx;
      font-family: OPPOSans, OPPOSans-Heavy;
      font-weight: 800;
      text-align: CENTER;
      color: #303030;
    }

    .nickname {
      font-size: 40rpx;
      font-family: SF Pro Text, SF Pro Text-Bold;
      font-weight: 700;
      text-align: CENTER;
      color: #303030;
      line-height: 40rpx;
    }

    .follow {
      font-size: 30rpx;
      font-family: OPPOSans, OPPOSans-Heavy;
      font-weight: 800;
      text-align: CENTER;
      color: #303030;
    }
  }

  /** 用户动态信息 **/
  .user-info-content {
    width: 100%;
    display: flex;
    flex-direction: column;
    align-items: center;
    margin-top: 80rpx;

    .info-top {
      width: 90%;
      display: flex;
      flex-direction: row;
      justify-content: space-around;

      .user-info-item {
        width: 45%;
        display: flex;
        flex-direction: row;
        background: #f6f5f4;
        border-radius: 40rpx;
        height: 180rpx;
        margin-bottom: 30rpx;

        .user-info-image {
          width: 40%;
          display: flex;
          flex-direction: column;
          align-items: center;
          justify-content: center;

          .image-content {
            width: 80rpx;
            height: 80rpx;
            border-radius: 60rpx;
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
          }

          .p1 {
            background: #45b26b;
          }

          .p2 {
            background: #9757d7;
          }

          .p3 {
            background: #3772ff;
          }

          .p4 {
            background: #4bc9f0;
          }

          image {
            width: 35rpx;
            height: 35rpx;
          }
        }

        .user-info-data {
          width: 60%;
          display: flex;
          flex-direction: column;
          align-items: flex-start;
          justify-content: center;

          .user-info-flag {
            font-size: 25rpx;
            font-family: OPPOSans, OPPOSans-Medium;
            font-weight: 500;
            text-align: LEFT;
            color: #353945;
            line-height: 40rpx;
          }

          .user-info-word {
            font-size: 35rpx;
            font-family: OPPOSans, OPPOSans-Heavy;
            font-weight: 800;
            text-align: LEFT;
            color: #353945;
            line-height: 28rpx;
          }

        }
      }
    }
  }
}
</style>